import {arrayToObject} from '../../../../assets/js/util.js' 
export default{
	    data() {
	      return{
			title:'实时强化流水',
			xlsxTitle:'',
			// 默认参数 和 URL
			SRC: '',
			DP: {
				p: '',
				f: ''
			},
			loading:false,
			searchable:false,
			pickerOptions: {
	          	shortcuts: [{
		            text: '最近一周',
		            onClick(picker) {
		              const end = new Date();
		              const start = new Date();
		              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
		              picker.$emit('pick', [start, end]);
		            }
		          }, {
		            text: '最近15天',
		            onClick(picker) {
		              const end = new Date();
		              const start = new Date();
		              start.setTime(start.getTime() - 3600 * 1000 * 24 * 15);
		              picker.$emit('pick', [start, end]);
		            }
		          }, {
		            text: '最近一个月',
		            onClick(picker) {
		              const end = new Date();
		              const start = new Date();
		              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
		              picker.$emit('pick', [start, end]);
		            }
	          }]
	        },
			form: {
			  	date: '',

			  	dateStart:'',
			  	dateEnd:'',
				// 选择的平台
				plat:'0',
				plats:[],
				// 选择的渠道
				channel:'0',
				channels:[],
				// 选择的区服
				serviceid:'0',
				serviceids:[],

				userid:'',
				roleid:''			
			},
			inputWidth:'220px',
			formLabelWidth: '100px',
	      	/*当前页显示数量*/
	      	pageSize:10,
	      	/*每页显示的数量*/
	      	pageSizes:[10, 20, 50, 100],
	      	/*dataTotal表数量*/
	      	dataTotal:0,
	      	/*表格数据*/
	      	realTimeServiceTableData:[],
	      	realTimeServiceTableDatas:[],
	      	/*realTimeServiceContent 的宽*/
	      	realTimeServiceContent:'100%',
	      	/*表的宽度*/
	      	realTimeServiceTableWidth: "100%",
	      	/*数据表头*/
			realTimeServiceTableHead:[
				{prop:'innerId',label:'时间',width:'160'},
				{prop:'plat',label:'平台',width:'120'},
				{prop:'channel',label:'渠道',width:'160'},
				{prop:'serviceArea',label:'区服',width:'160'},
				{prop:'accountId',label:'账号ID',width:'120'},
				{prop:'roleId',label:'角色ID',width:'160'},
				{prop:'roleGrade',label:'角色等级',width:'160'},
				{prop:'VIPGrade2',label:'类型',width:'160'},
				{prop:'VIPGrade3',label:'对象名称',width:'160'},
				{prop:'VIPGrade4',label:'强化前等级',width:'160'},
				{prop:'VIPGrade5',label:'强化后等级',width:'160'},
				{prop:'rechargeAll',label:'操作',width:'160'}

	      	],
	      	rules: {
			    plat: [
			          { required: true, message: '请选择平台', trigger: 'change' }
			        ],
			    channel: [
			          { required: true, message: '请选择渠道', trigger: 'change' }
			        ],
			    serviceid: [
			          { required: true, message: '请选择区服', trigger: 'change' }
			        ]
			},
	        /*分页器*/
	        currentPage: 1
	      }
	    },
		computed:{
			// xlsx(){
			// 	return this.title+'.xlsx'
			// }
		},
	    methods: {
			successFun(data) {
		      this.$message({
		        message: data,
		        type: 'success'
		      });
		    },
	    	errorFun(data) {
		      this.$message.error(data);
		    },
	    	dateChange(val){
	    		var t = val.split(' - ')
	    		this.form.dateStart = t[0];
	    		this.form.dateEnd = t[1];
	    	},
	    	loads(){
	    		this.loading=!this.loading;	//加载中
				this.searchable = !this.searchable;//查询按钮不可用
	    	},
	    	// 平台切换 渠道切换 区服切换
			selectPlats(val) {
				this.reGetData(`updatePlatform.do?platformid=${val}`)
			},
			selectChannels(val) {
				this.reGetData(`updateChannel.do?channelid=${val}`)
			},
			selectServers(val) {
				this.reGetData(`updateServer.do?serverid=${val}`)
			},
	    	reset(form) {
		    	this.$refs[form].resetFields();
		    },
		    search(form){
				this.$refs[form].validate((valid) => {
		        if (valid) {
		         	this.commitData()
		        } else {
		          this.errorFun('请完善输入的值！');
		          return false;
		        }
		      })
			},
			commitData(){
				this.realTimeServiceTableDatas=[];
				this.realTimeServiceTableData=[];
				let obj={
					dateStart:this.form.dateStart,
					dateEnd:this.form.dateEnd,
					userid:this.form.userid,
					roleid:this.form.roleid,
					platform:this.form.plat,
					channel:this.form.channel,
					serverid:this.form.serviceid
				}
				obj = encodeURI(JSON.stringify(obj));
				this.loads(); //加载 查询按钮不可用
				this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=search`,{
					params:{'ps':'params='+obj},
				}).then(function(res){
					this.loads();
	    			var data =  JSON.parse(JSON.parse(res.body))
	    			if(parseInt(data.res) === 0){
	    				if(data.data.length==0){
	    					this.errorFun('查询结果为空！')
	    				}else{
	  						this.realTimeServiceTableDatas =arrayToObject(this.realTimeServiceTableHead,data.data);
	    				}
		  			}else{
		  				this.errorFun(data.msg)
		  			}
		  			this.dataTotal = this.realTimeServiceTableDatas.length;
	    			this.realTimeServiceTableData = this.realTimeServiceTableDatas.slice((this.currentPage-1)*this.pageSize,this.currentPage*this.pageSize)
	    		})
			},
			// 表格下载
		  	excel(val) {
				this.xlsxTitle = val+"-"+this.form.dateStart+"到"+this.form.dateEnd;

				this.downloadExl(this.realTimeServiceTableHead,this.realTimeServiceTableDatas,this.xlsxTitle,this.title);
		  	},
	    	/*分页器*/
			handleSizeChange(val) {
				this.pageSize = val;
				// this.getData(this.pageSize,this.currentPage)
				this.realTimeServiceTableData = this.realTimeServiceTableDatas.slice((this.currentPage-1)*this.pageSize,this.currentPage*this.pageSize)
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				// this.getData(this.pageSize,this.currentPage)
				this.realTimeServiceTableData = this.realTimeServiceTableDatas.slice((this.currentPage-1)*this.pageSize,this.currentPage*this.pageSize)
			},
	    	/*获取数据*/
	    	getData: function(){
	    		this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=initParam`,{
					params:{'ps': '' }
				}).then(function(res){
	    			var data =  JSON.parse(JSON.parse(res.body))
	    			if(parseInt(data.res) == 0){
	    				this._dataPush(data.channelItmes,this.form.channels)
		    			this._dataPush(data.serverItmes,this.form.serviceids)
		    			this._dataPush(data.platformItmes,this.form.plats)
	    			}else{
	    				this.getErrorMsg(Number(data.res))
	    			}
	    			// this.form.channel = {label:this.form.channels[0]['label'],value:this.form.channels[0]['label']}
	    		})
	    	},
	    	/*重新获取数据*/
	    	reGetData: function(url){
	    		this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=${url}`,{
					params:{'ps': '' }
				}).then(function(res){
	    			var data =  JSON.parse(JSON.parse(res.body))
	    			if(data.res == 0){
	    				if(url.indexOf('serverid')!=-1){
	    					this.form.channels=[]
	    					this.form.plats=[]
		    				this._dataPush(data.channelItmes,this.form.channels)
			    			this._dataPush(data.platformItmes,this.form.plats)
	    				}else if(url.indexOf('platformid')!=-1){
	    					this.form.channels=[]
	    					this.form.serviceids=[]
	    					this._dataPush(data.channelItmes,this.form.channels)
		    				this._dataPush(data.serverItmes,this.form.serviceids)
	    				}else if(url.indexOf('channelid')!=-1){
	    					this.form.serviceids=[]
	    					this.form.plats=[]
			    			this._dataPush(data.serverItmes,this.form.serviceids)
			    			this._dataPush(data.platformItmes,this.form.plats)
	    				}
	    			}else{
	    				this.errorFun(data.msg)
	    			}
	    			// this.form.channel = {label:this.form.channels[0]['label'],value:this.form.channels[0]['label']}
	    		})
	    	},
	    	_dataPush(firstData,lastData){
				for (var key in firstData){
			        lastData.push({label:key,value:firstData[key]})
				}
	    	},
	    	/*设置初始值*/
	    	_init(){
	    		window.addEventListener('resize', function() {
	    			/*修改表格的宽度*/
	          		this.realTimeServiceTableWidth = document.body.clientWidth-240+"px";
	          		this.realTimeServiceContent = document.body.clientWidth-220+"px";
	        	}.bind(this))
	    	},
	    	/*选择平台切换*/
			head_choose_plat_fun(index) {
				for(var i=0; i<this.head_choose_plat.length; i++){
					this.head_choose_plat[i].activeIndex = false;
				}
				this.head_choose_plat[index].activeIndex = true;
			},
			head_choose_time_fun(index) {
				for(var i=0; i<this.head_choose_time.length; i++){
					this.head_choose_time[i].activeIndex = false;
				}
				this.head_choose_time[index].activeIndex = true;
			},
	    },
	    mounted() {
	     	this._init()
	     	// 初始化URL
			this.SRC = this.$store.getters.src;
			this.DP.p = localStorage.getItem('ZULONGBIPROJECTID');
			this.DP.f = localStorage.getItem('ZULONGBIPAGEID');
	     	this.getData()
	     	this.form.date = [new Date(new Date().getTime() - 86400000),new Date()];
	     	// this.getData(this.pageSize,this.currentPage)
	    }
	}